package com.apes.fn.rpt.profit.grossProfitProcess.processor;

import com.apes.fn.rpt.profit.grossProfitProcess.CommonGrossProfitProcess;
import com.apes.fn.rpt.profit.grossProfitProcess.GrossProfitProcess;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 定损接案介绍费
 */
@Component
public class LossSettleExpense extends CommonGrossProfitProcess implements GrossProfitProcess {

    @Override
    public String getSQL(Date taskDate){

        return "select * " +
                "from (SELECT X.CREATE_DATE, " +
                "       '定损'                                           INCOME_CATEGORIES, " +
                "       '接案介绍费'                                        SERVE_TYPE_NAME, " +
                "       X.DEPT_ID, " +
                "       X.COMPANY_ID, " +
                "       SUM(X.PRICE_TOTAL)                             AMOUNT, " +
                "       0                                              TIMES, " +
                "       SUM(X.PRICE_TOTAL)                             PRICE_TOTAL, " +
                "       SUM(X.COST)                                    COST, " +
                "       0                                              vip_in, " +
                "       0                                              vip_out, " +
                "       0                                              CONSUMABLE_USE, " +
                "       0                                              ACCESSORY_COST, " +
                "       SUM(X.COST)                                              MAN_HOUR_COST, " +
                "       SUM(X.PLATFORM_MANAGEMENT_FEE)                 PLATFORM_MANAGEMENT_FEE, " +
                "       0                                              SUPPLY_CHAIN_MANAGEMENT_FEE" +
                " FROM (SELECT TRUNC(A.CREATE_DATE) CREATE_DATE, " +
                "             A.DEPT_ID, " +
                "             A.PAYMENT_ID         COMPANY_ID, " +
                "             A.AMOUNT             PRICE_TOTAL, " +
                "             0                    COST, " +
                "             0                    PLATFORM_MANAGEMENT_FEE " +
                "      FROM FN_SETTLE_EXPENSE A " +
                "      WHERE A.EXPENSE_TYPE = 'infoExpense' " +
                "        AND A.TYPE = 'in' " +
                "        AND A.DEPT_ID IS NOT NULL " +
                "        AND A.CREATE_DATE >= to_date(:P_DATE, 'YYYY-mm-dd') " +
                "        AND A.CREATE_DATE < to_date(:P_DATE, 'YYYY-mm-dd') + 1 " +
                "      UNION ALL " +
                "      SELECT TRUNC(A.CREATE_DATE) CREATE_DATE, " +
                "             A.DEPT_ID, " +
                "             A.PAYMENT_ID         COMPANY_ID, " +
                "             0                    PRICE_TOTAL, " +
                "             A.AMOUNT             COST, " +
                "             0                    PLATFORM_MANAGEMENT_FEE " +
                "      FROM FN_SETTLE_EXPENSE A " +
                "      WHERE A.EXPENSE_TYPE = 'infoExpense' " +
                "        AND A.TYPE = 'out' " +
                "        AND A.DEPT_ID IS NOT NULL " +
                "        AND A.CREATE_DATE >= to_date(:P_DATE, 'YYYY-mm-dd') " +
                "        AND A.CREATE_DATE < to_date(:P_DATE, 'YYYY-mm-dd') + 1 " +
                "      UNION ALL " +
                "      SELECT TRUNC(A.CREATE_DATE) CREATE_DATE, " +
                "             A.DEPT_ID, " +
                "             A.PAYMENT_ID         COMPANY_ID, " +
                "             0                    PRICE_TOTAL, " +
                "             0                    COST, " +
                "             (CASE WHEN TYPE ='in' then -1* A.AMOUNT else A.AMOUNT end )             PLATFORM_MANAGEMENT_FEE " +
                "      FROM FN_SETTLE_EXPENSE A " +
                "      WHERE A.EXPENSE_TYPE = 'platformExpense' " +
                "        AND A.DEPT_ID IS NOT NULL " +
                "        AND (SELECT D.COMPANY_ID FROM MAS_DEPT d WHERE D.ID = a.DEPT_ID) = a.PAYMENT_ID " +
                "        AND A.CREATE_DATE >= to_date(:P_DATE, 'YYYY-mm-dd') " +
                "        AND A.CREATE_DATE < to_date(:P_DATE, 'YYYY-mm-dd') + 1 " +
                "        UNION ALL " +
                "      SELECT TRUNC(A.CREATE_DATE) CREATE_DATE, " +
                "             A.DEPT_ID, " +
                "             A.PAYMENT_ID         COMPANY_ID, " +
                "             0                    PRICE_TOTAL, " +
                "             A.AMOUNT                    COST, " +
                "             0             PLATFORM_MANAGEMENT_FEE " +
                "      FROM FN_SETTLE_EXPENSE A " +
                "      WHERE A.EXPENSE_TYPE = 'lossQuotationExpense' " +
                "        AND A.TYPE = 'out' " +
                "        AND A.DEPT_ID IS NOT NULL " +
                "        AND (SELECT D.COMPANY_ID FROM MAS_DEPT d WHERE D.ID = a.DEPT_ID) = a.PAYMENT_ID " +
                "        AND A.CREATE_DATE >= to_date(:P_DATE, 'YYYY-mm-dd') " +
                "        AND A.CREATE_DATE < to_date(:P_DATE, 'YYYY-mm-dd') + 1) X " +
                "GROUP BY X.CREATE_DATE, X.DEPT_ID, X.COMPANY_ID)";
    }

    @Override
    public boolean support(String type) {
        return "LossSettle".equals(type);
    }
}
